//
//  FQDMainMenuItemsCollection.m
//  FakeQDaily
//
//  Created by mewxiv on 10/5/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDMainMenuItemsCollection.h"
#import "FQDMenuItem.h"
#import "QDailyAPIConstants.h"
#import "FQDRestModel.h"
#import "FQDSimpleFeedCellItemProvider.h"

// 导航
#import "FQDNavigationController.h"
#import "FQDMainViewController.h"
#import "FQDInfoWebViewController.h"
// 账号登录
#import "FQDAccountManager.h"
#import "FQDRegisterNavViewController.h"
// 主页
#import "FQDHomeFeedViewController.h"
// 主菜单
#import "FQDMainMenuViewController.h"
#import "FQDMainQButton.h"
// 浏览文章
#import "FQDArticleFeedBaseViewController.h"
// 新闻分类
#import "FQDNewsCategoryFeedCellItemProvider.h"
// 栏目
#import "FQDColumnCenterViewController.h"
// 好奇心研究所
#import "FQDLabPapersFeedCellItemProvider.h"
// 我的消息
#import "FQDMyMessageViewController.h"
// 个人中心
#import "FQDUserCenterViewController.h"

@interface FQDMainMenuItemsCollection ()

@property (nonatomic, weak) FQDMainMenuViewController *mainMenuVC;

@end

@implementation FQDMainMenuItemsCollection

+ (instancetype)collectionWithMainMenuViewController:(FQDMainMenuViewController *)mainMenuVC
{
    FQDMainMenuItemsCollection *collection = [[self alloc] init];
    collection.mainMenuVC = mainMenuVC;
    return collection;
}

- (FQDMenuItemContainer *)categoryMenuItem
{
    FQDMenuItemContainer *item_category = [FQDMenuItemContainer menuItemWithTitle:@"新闻分类"
                                           iconName:@"menu_category"];
    [self fetchSubMenuDataForCategoryItem:item_category];
    return item_category;
}

- (void)fetchSubMenuDataForCategoryItem:(FQDMenuItemContainer *)categoryItem
{
    [[QDailyAPI sharedAPI] getSideBar:^(id responseObject, NSError *error) {
        NSMutableArray *subItems = [NSMutableArray array];
        NSArray *categoryList = [NSArray yy_modelArrayWithClass:[FQDNewsCategory class]
                                                           json:responseObject[@"response"]];
        for (FQDNewsCategory *category in categoryList) {
            FQDActionMenuItem *item = [FQDActionMenuItem menuItemWithTitle:category.title
                                                                   iconURL:category.normal];
            [item setSelectAction:^{
                NSString *const kNewsCategoryFeed = @"category_feed";
                FQDSimpleFeedCellItemProvider *provider = [[FQDSimpleFeedCellItemProvider alloc] init];
                provider.cacheId = kNewsCategoryFeed;
                provider.feedId = category.ID;
                [provider setFetchFeedBlock:^(NSString *feedId, NSString *cursorKey, QDailyAPICompleteBlock completion) {
                    [[QDailyAPI sharedAPI] getNewsCategoriesFeed:feedId key:cursorKey completion:completion];
                }];
                
                FQDArticleFeedBaseViewController *afbVC = [[FQDArticleFeedBaseViewController alloc] init];
                afbVC.title = category.title;
                afbVC.provider = provider;
                [self.mainMenuVC dismissSelfAndPushViewController:afbVC animated:YES];
            }];
            [subItems addObject:item];
        }
        categoryItem.subItems = [subItems copy];
    }];
}

- (FQDActionMenuItem *)aboutMenuItem
{
    FQDActionMenuItem *item_about = [FQDActionMenuItem menuItemWithTitle:@"关于我们" iconName:@"menu_about"];
    [item_about setSelectAction:^{
        FQDInfoWebViewController *infoVC = [[FQDInfoWebViewController alloc] init];
        infoVC.title = @"关于我们";
        infoVC.urlString = QDailyAboutUsURL;
        
        FQDNavigationController *navController = [[FQDNavigationController alloc] initWithRootViewController:infoVC];
        navController.edgesForExtendedLayout = UIRectEdgeNone;
        [self.mainMenuVC dismissSelfAndPresentViewController:navController animated:YES];
    }];
    return item_about;
}

- (FQDActionMenuItem *)columnMenuItem
{
    FQDActionMenuItem *item_column = [FQDActionMenuItem menuItemWithTitle:@"栏目中心" iconName:@"menu_column"];
    [item_column setSelectAction:^{
        FQDColumnCenterViewController *ccVC = [[FQDColumnCenterViewController alloc] init];
        [self.mainMenuVC dismissSelfAndPushViewController:ccVC animated:YES];
    }];
    return item_column;
}

- (FQDActionMenuItem *)labMenuItem
{
    FQDActionMenuItem *item_lab = [FQDActionMenuItem menuItemWithTitle:@"好奇心研究所" iconName:@"menu_lab"];
    [item_lab setSelectAction:^{
        FQDLabPapersFeedCellItemProvider *provider = [[FQDLabPapersFeedCellItemProvider alloc] init];
        FQDArticleFeedBaseViewController *afbVC = [[FQDArticleFeedBaseViewController alloc] init];
        afbVC.title = @"好奇心研究所";
        afbVC.provider = provider;
        [self.mainMenuVC dismissSelfAndPushViewController:afbVC animated:YES];
    }];
    return item_lab;
}

- (FQDActionMenuItem *)messageMenuItem
{
    FQDActionMenuItem *item_message = [FQDActionMenuItem menuItemWithTitle:@"我的消息" iconName:@"menu_noti"];
    [item_message setSelectAction:^{
        FQDAccountManager *accountManager = [FQDAccountManager sharedManager];
        if ([accountManager isLogined]) {
            FQDMyMessageViewController *mmVC = [[FQDMyMessageViewController alloc] init];
            [self.mainMenuVC dismissSelfAndPushViewController:mmVC animated:YES];
        } else {
            FQDMainViewController *mainVC = (FQDMainViewController *)self.mainMenuVC.parentViewController;
            FQDRegisterNavViewController *navController = [[FQDRegisterNavViewController alloc] init];
            navController.backgroundViewController = mainVC.homeFeedVC;
            [self.mainMenuVC dismissSelfAndPresentViewController:navController animated:YES];
        }
    }];
    return item_message;
}

- (FQDActionMenuItem *)userCenterMenuItem
{
    FQDActionMenuItem *item_user = [FQDActionMenuItem menuItemWithTitle:@"个人中心" iconName:@"menu_user"];
    [item_user setSelectAction:^{
        FQDUserCenterViewController *userCenterVC = [[FQDUserCenterViewController alloc] init];
        [self.mainMenuVC dismissSelfAndPushViewController:userCenterVC animated:YES];
    }];
    return item_user;
}

- (FQDActionMenuItem *)homeMenuItem
{
    FQDActionMenuItem *item_home = [FQDActionMenuItem menuItemWithTitle:@"首页" iconName:@"menu_home"];
    [item_home setSelectAction:^{
        [self.mainMenuVC dismissSelfAnimated];
    }];
    return item_home;
}

@end
